/*
 * File which contains functions to be used to process multi-valued attributes
 * in edition forms (creation and modification).
 * Such attributes are displayed as follows :
 *   - a hidden field contains a '#' separated list of values of the attribute,
 *     values when form is loaded. Its name must be the name of the attribute.
 *   - a select box contains the current list of values. Its name must be the name
 *     of the attribute, prefixed with 'x'. Example : xdescription for description.
 *   - a text field contains the next value. Its name must be the name of the attribute,
 *     prefixed with 'n'. Example : ndescription for description.
 *   - a button allows to add the next value to the list of current values
 *   - a button allows to suppress the selected value in the list
 * To include this in the JSP pages, use Struts HTML rewrite tag.
 * Example : <script language="Javascript1.1" src="<html:rewrite page='/edition.js'/>"> </script>
 *
 */

/*
 * This function has to be called to add the next value to the list of current values
 * Parameter : field : name of the attribute.
 * Example : <input type="button" value="Add" onClick="doAdd('description')">
 *
 */
function doAdd(form,field)
{
   if (document.forms[form].elements["n" + field].value == "")
   {
      return
   }
   
   var len2 = document.forms[form].elements["x" + field].length
   for (i = 0; i < len2 ; i++)
   {
      if (
      document.forms[form].elements["x" + field].options[i].value.toLowerCase() ==
                  document.forms[form].elements["n" + field].value.toLowerCase() )
      {
         document.forms[form].elements["n" + field].value = ""
         return         
      }    
   }
   
   var newoption = new Option (document.forms[form].elements["n" + field].value,
                               document.forms[form].elements["n" + field].value);
   var len = document.forms[form].elements["x" + field].length
   document.forms[form].elements["x" + field].options[len] = newoption;
   document.forms[form].elements["x" + field].options[len].selected = true;
   document.forms[form].elements["n" + field].value = ""
}

/*
 * This function has to be called to remove the selected value from the list of current 
 * values.
 * Parameter : field : name of the attribute
 * Example <input type="button" value="Remove" onClick="doRemove('description')">
 *
 */
function doRemove(form, field)
{
   if (document.forms[form].elements["x" + field].selectedIndex == -1)
   {
      return;
   }
   var len = document.forms[form].elements["x" + field].length - 1
   for (i = document.forms[form].elements["x" + field].selectedIndex; i < len ; i++)
   {
      document.forms[form].elements["x" + field].options[i].text =
                  document.forms[form].elements["x" + field].options[i+1].text
      document.forms[form].elements["x" + field].options[i].value =
                  document.forms[form].elements["x" + field].options[i+1].value
   }
   document.forms[form].elements["x" + field].length--
}

/*
 * This function has to be called before the submission of the form, for each concerned attribute.
 * Parameter : field : name of the attribute
 *
 */
function doMaj(form, field)
{
   var result = "";
   var len = document.forms[form].elements["x" + field].length
   for (i = 0; i < len ; i++)
   {
      result += "#" + document.forms[form].elements["x" + field].options[i].value;
   }
   document.forms[form].elements[field].value = result;
}
